//###########################################################################
//
// Notes:
//###########################################################################
#include "stm32f4xx.h"
#include "stm32f4xx_can.h"
#include "Stm32f4xx_flash.h"
#include "flash.h"
#include "mainInclude.h"
#include "iap.h"
#include "string.h"

#define CMD_GET_VERSION          0xFE    // 读取信息	0xFE
#define CMD_UPGRADE_LOCK         0xCA    // 升级开锁	0xCA
#define CMD_UPGRADE_DIVCE_CODE   0xCE    // 请求节点上传设备码	0xCE
#define CMD_UPGRADE_START        0xBE    // 开始升级请求	0xBE
#define CMD_UPGRADE_DATA         0xBC    // 写升级数据	0xBC
#define CMD_UPGRADE_END          0xBA    // 升级结束	0xBA

extern uint8_t   gPCCOMM_RxValueTab[512];
extern uint8_t   gPCCOMM_TxValueTab[512];
extern uint16_t  MCU_TX;

uint8_t SciRx[512];
uint16  SciRxCounter=0;
SciLoadInfo SciLoadInfoUse={0};
CanLoadInfo CanLoadInfoUse={0};
u32	        uds_crc = 0xffffffff;
u32	        uds_can_crc = 0xffffffff;

extern void UpdateDevInfo(void);
extern void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u32 NumToWrite);  
extern void SendOneMessage( J1939_MESSAGE *MsgPtr,Uint16 Id);

//lastCRCvalue 初始值=0xffffffff;
/*uint32 CRC32( u8* pData, uint32 nLength, uint32 lastCRCvalue)
{
    u8 i; 
	u8 xbit=0;
	u8 data;
	uint32 crc = lastCRCvalue;	 
	while(nLength != 0)    
	{	
		crc ^= *pData;
		
		for(i=0; i< 8; i++)	 
		{	
			if(crc & 1)	
			{	
				crc >>= 1;	 
				crc ^= 0x04C11DB7;	 
			}	
			else
			{
				crc >>= 1;
			}
					 
		}
        pData = &pData[1];
		nLength--;
	}
		
	return(crc);   
}*/

//lastCRCvalue 初始值=0xffffffff;
uint32 CRC32( u8* pData, uint32 nLength, uint32 lastCRCvalue)
{
  u32 xbit;
  u32 data;
  u32 dwPolynomial = 0x04c11db7;
  int32_t k=0,len=0;
  int32_t bits=0;
  u32 crc = lastCRCvalue;	

  len = nLength/4;
  while(len != 0)    
	{	
		xbit = (u32)1 << 31;
		data = ((u32)(*(pData+k*4 + 3)&0xff)<<24) + ((u32)(*(pData+k*4 + 2)&0xff)<<16) + \
			   ((u32)(*(pData+k*4 + 1)&0xff)<<8)  + ((u32)(*(pData+k*4 + 0)&0xff)<<0);

		for (bits = 0; bits < 32; bits++) 
		{
           if (crc & 0x80000000)
           	{
               crc <<= 1;
			   crc ^= dwPolynomial;
		    }
		   else
		   	{
               crc <<= 1;
		    }

		    if (data & xbit)
		    {
               crc ^= dwPolynomial;
			}

			xbit >>= 1;
		}
		
		len--;
		k++;
	}

  return(crc);  
}

//CAN升级主逻辑
extern uint16_t  gCanMCURXOkFlagG0;
extern uint8_t   gCanMCURXG0[8];
void CanUpLoadMain(void)
{
  Uint8 RD[8]={0};
  uint32 rcrc=0;
  Uint8  uTemp8=0,uTemp8_1=0,uTemp8_2=0;
  uint32_t secstart;
  uint32_t secend;
  uint32_t reciveaddr;
  uint32   k=0;

    if(0==gCanMCURXOkFlagG0)
    	{return;}
	gCanMCURXOkFlagG0 = 0;
  
	RD[0] =  gCanMCURXG0[0]&0x00ff;
	RD[1] =  gCanMCURXG0[1]&0x00ff;	
	RD[2] =  gCanMCURXG0[2]&0x00ff;
	RD[3] =  gCanMCURXG0[3]&0x00ff;
	RD[4] =  gCanMCURXG0[4]&0x00ff;
	RD[5] =  gCanMCURXG0[5]&0x00ff;
	RD[6] =  gCanMCURXG0[6]&0x00ff;
	RD[7] =  gCanMCURXG0[7]&0x00ff;

	switch(RD[0])
	{
     /********************************升级相关接口****************************************/
     case  CMD_GET_VERSION:
		OneMessage.FormatMsg.DataLength = 8;	
     	OneMessage.FormatMsg.Data[0]  = ((DevInfoInit.HardVersion[1]<<8)&0xff00) + ((0xff<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[1]  = ((DevInfoInit.HardVersion[3]<<8)&0xff00) + ((DevInfoInit.HardVersion[2]<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[2]  = ((DevInfoInit.SoftVerSion[1]<<8)&0xff00) + ((DevInfoInit.SoftVerSion[0]<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[3]  = ((DevInfoInit.SoftVerSion[3]<<8)&0xff00) + ((DevInfoInit.SoftVerSion[2]<<0)&0x00ff);     
     	SendOneMessage(&OneMessage,MCU_TX);
        break;

	 case  CMD_UPGRADE_LOCK:
		OneMessage.FormatMsg.DataLength = 3;	
     	OneMessage.FormatMsg.Data[0]  = ((UniqueSerialNumber<<8)&0xff00) + ((0xCB<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[1]  = ((0<<8)&0xff00) + ((0x01<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[2]  = ((0<<8)&0xff00) + ((0<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[3]  = ((0<<8)&0xff00) + ((0<<0)&0x00ff);     
     	SendOneMessage(&OneMessage,MCU_TX);
        break;

	case  CMD_UPGRADE_DIVCE_CODE:
		OneMessage.FormatMsg.DataLength = 8;	
     	OneMessage.FormatMsg.Data[0]  = (((uint16)DeviceInfo[0]<<8)&0xff00) + ((0xCF<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[1]  = (((uint16)DeviceInfo[2]<<8)&0xff00) + (((uint16)DeviceInfo[1]<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[2]  = (((uint16)DeviceInfo[4]<<8)&0xff00) + (((uint16)DeviceInfo[3]<<0)&0x00ff);
     	OneMessage.FormatMsg.Data[3]  = (((uint16)DeviceInfo[6]<<8)&0xff00) + (((uint16)DeviceInfo[5]<<0)&0x00ff);     
     	SendOneMessage(&OneMessage,MCU_TX);
        break;

	case  CMD_UPGRADE_START:
        if(RD[1] == (UniqueSerialNumber & 0xff) && 0==CanLoadInfoUse.LoadStep)
        {
             OneMessage.FormatMsg.DataLength = 7;	
        	 OneMessage.FormatMsg.Data[0]  = ((0x0001<<8)&0xff00) + ((0xBF<<0)&0x00ff);
        	 OneMessage.FormatMsg.Data[1]  = (((uint16)RD[3]<<8)&0xff00) + (((uint16)RD[2]<<0)&0x00ff);
        	 OneMessage.FormatMsg.Data[2]  = ((DevInfoInit.HardVersion[3]<<8)&0xff00) + (((uint16)RD[4]<<0)&0x00ff);
        	 OneMessage.FormatMsg.Data[3]  = ((0<<8)&0xff00) + ((DevInfoInit.HardVersion[2]<<0)&0x00ff);     
        	 SendOneMessage(&OneMessage,MCU_TX);
			 
             secstart = GetSector(APP_START_ADDRESS);
    		 secend = GetSector(FMC_FLASH_END);
			 uTemp8_2 = (secend - secstart)/8;
			 uTemp8 = 0;
    		 TIM_Cmd(TIM6,DISABLE);
    		 for (k = secstart; k < secend; k += 8) 
              {
                FLASH_Unlock();
    			FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR\
    				            |FLASH_FLAG_PGAERR|FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
                FLASH_EraseSector(k,VoltageRange_3);
    			FLASH_Lock();

				uTemp8++;
				uTemp8_1 = (int32)uTemp8*100/uTemp8_2 + 100;
				if(k >= secend || uTemp8_1 >= 200)
				  {uTemp8_1 = 200;}
				OneMessage.FormatMsg.DataLength = 7;	
        	    OneMessage.FormatMsg.Data[0]  = ((uTemp8_1<<8)&0xff00) + ((0xBF<<0)&0x00ff);
        	    OneMessage.FormatMsg.Data[1]  = (((uint16)RD[3]<<8)&0xff00) + (((uint16)RD[2]<<0)&0x00ff);
        	    OneMessage.FormatMsg.Data[2]  = ((DevInfoInit.HardVersion[3]<<8)&0xff00) + (((uint16)RD[4]<<0)&0x00ff);
        	    OneMessage.FormatMsg.Data[3]  = ((0<<8)&0xff00) + ((DevInfoInit.HardVersion[2]<<0)&0x00ff);     
        	    SendOneMessage(&OneMessage,MCU_TX);
    		  }    
             DevInfoCur.AppUpgradeFlag = 1;
			 UpdateDevInfo();
			 TIM_Cmd(TIM6,ENABLE);
             OneMessage.FormatMsg.DataLength = 7;	
        	 OneMessage.FormatMsg.Data[0]  = ((0x0006<<8)&0xff00) + ((0xBF<<0)&0x00ff);
        	 OneMessage.FormatMsg.Data[1]  = (((uint16)RD[3]<<8)&0xff00) + (((uint16)RD[2]<<0)&0x00ff);
        	 OneMessage.FormatMsg.Data[2]  = ((DevInfoInit.HardVersion[3]<<8)&0xff00) + (((uint16)RD[4]<<0)&0x00ff);
        	 OneMessage.FormatMsg.Data[3]  = ((0<<8)&0xff00) + ((DevInfoInit.HardVersion[2]<<0)&0x00ff);     
        	 SendOneMessage(&OneMessage,MCU_TX);
			 CanLoadInfoUse.AppLen = (((uint32)RD[4]<<16)&0xff0000) + (((uint32)RD[3]<<8)&0xff00) + (((uint32)RD[2]<<0)&0x00ff);
             CanLoadInfoUse.ReciveLen = 0;
			 CanLoadInfoUse.RxSum = 0;
			 CanLoadInfoUse.LoadStep = 1;
			 CanLoadInfoUse.LoadResetCounter = 0;
			 CanLoadInfoUse.LoadPadeCounter = 0;
			 uds_can_crc = 0xffffffff;
        }
        break;

    case  CMD_UPGRADE_DATA:
		if(DevInfoCur.AppUpgradeFlag && CanLoadInfoUse.LoadStep)
		{
            CanLoadInfoUse.LoadResetCounter = 0;
			reciveaddr = ((uint32)RD[3]<<16) + ((uint32)RD[2]<<8) + ((uint32)RD[1]<<0);
			
			if(reciveaddr == CanLoadInfoUse.ReciveLen)
			{
			    CanLoadInfoUse.RxData[CanLoadInfoUse.RxSum++] = RD[4];
			    CanLoadInfoUse.RxData[CanLoadInfoUse.RxSum++] = RD[5];
			    CanLoadInfoUse.RxData[CanLoadInfoUse.RxSum++] = RD[6];
			    CanLoadInfoUse.RxData[CanLoadInfoUse.RxSum++] = RD[7];
                CanLoadInfoUse.ReciveLen += 4;
    			if(CanLoadInfoUse.RxSum>=64)
    			{
    			   CanLoadInfoUse.RxSum = 0;
    			   for(k=0;k<32;k++)
                   {
                      CanLoadInfoUse.RxDataHalfWord[k] = (((uint16)CanLoadInfoUse.RxData[1+k*2]<<8)&0xff00)\
     				 	                               + ((uint16)CanLoadInfoUse.RxData[0+k*2]&0x00ff);
     			   } 
                   TIM_Cmd(TIM6,DISABLE);						
    	           STMFLASH_Write_NoCheck(APP_START_ADDRESS + CanLoadInfoUse.LoadPadeCounter*64,&CanLoadInfoUse.RxDataHalfWord[0],32);            
    		       TIM_Cmd(TIM6,ENABLE);	
    			   CanLoadInfoUse.LoadPadeCounter++;
    			 
    			   //CRC计算
    		       uds_can_crc = CRC32(&CanLoadInfoUse.RxData[0], 64, uds_can_crc); 
    			}
    			else if(CanLoadInfoUse.ReciveLen >= CanLoadInfoUse.AppLen)
    			{
    			   for(k=0;k<CanLoadInfoUse.RxSum/2;k++)
                   {
                      CanLoadInfoUse.RxDataHalfWord[k] = (((uint16)CanLoadInfoUse.RxData[1+k*2]<<8)&0xff00)\
     				 	                               + ((uint16)CanLoadInfoUse.RxData[0+k*2]&0x00ff);
     			   } 
                   TIM_Cmd(TIM6,DISABLE);						
    	           STMFLASH_Write_NoCheck(APP_START_ADDRESS + CanLoadInfoUse.LoadPadeCounter*64,&CanLoadInfoUse.RxDataHalfWord[0],CanLoadInfoUse.RxSum/2);            
    		       TIM_Cmd(TIM6,ENABLE);	
    			   CanLoadInfoUse.LoadPadeCounter++;
    			 
    			   //CRC计算
    		       uds_can_crc = CRC32(&CanLoadInfoUse.RxData[0], CanLoadInfoUse.RxSum, uds_can_crc); 
    			}
			}			

			OneMessage.FormatMsg.DataLength = 8;	
        	OneMessage.FormatMsg.Data[0]  = ((CanLoadInfoUse.ReciveLen<<8)&0xff00) + ((0xBD<<0)&0x00ff);
        	OneMessage.FormatMsg.Data[1]  = (CanLoadInfoUse.ReciveLen>>8)&0xffff;
        	OneMessage.FormatMsg.Data[2]  = (((uint16)RD[5]<<8)&0xff00) + (((uint16)RD[4]<<0)&0x00ff);
        	OneMessage.FormatMsg.Data[3]  = (((uint16)RD[7]<<8)&0xff00) + (((uint16)RD[6]<<0)&0x00ff);     
        	SendOneMessage(&OneMessage,MCU_TX);
		}
        break;
		
    case  CMD_UPGRADE_END:
		rcrc = ((uint32)RD[4]<<24) + ((uint32)RD[3]<<16) + ((uint32)RD[2]<<8) + ((uint32)RD[1]<<0);
	    if( (rcrc == uds_can_crc)\
			 	&&( memcmp( DevInfoCur.HardVersion, ((uint8_t *)APP_CHECK_ADDRESS + 8), sizeof(DevInfoCur.HardVersion) ) == 0 ))
	    {
		  DevInfoCur.AppUpgradeFlag = 0;
	      UpdateDevInfo();   

		  OneMessage.FormatMsg.DataLength = 6;	
	      OneMessage.FormatMsg.Data[0]  = ((0x00<<8)&0xff00) + ((0xBB<<0)&0x00ff);
	      OneMessage.FormatMsg.Data[1]  = uds_can_crc&0xffff;
	      OneMessage.FormatMsg.Data[2]  = (uds_can_crc>>16)&0xffff;
	      OneMessage.FormatMsg.Data[3]  = (((uint16)0<<8)&0xff00) + (((uint16)0<<0)&0x00ff);     
	      SendOneMessage(&OneMessage,MCU_TX);
		}
		else if(rcrc != uds_can_crc)
		{
          OneMessage.FormatMsg.DataLength = 6;	
	      OneMessage.FormatMsg.Data[0]  = ((0x01<<8)&0xff00) + ((0xBB<<0)&0x00ff);
	      OneMessage.FormatMsg.Data[1]  = uds_can_crc&0xffff;
	      OneMessage.FormatMsg.Data[2]  = (uds_can_crc>>16)&0xffff;
	      OneMessage.FormatMsg.Data[3]  = (((uint16)0<<8)&0xff00) + (((uint16)0<<0)&0x00ff);     
	      SendOneMessage(&OneMessage,MCU_TX);
		}
		else
		{
          OneMessage.FormatMsg.DataLength = 6;	
	      OneMessage.FormatMsg.Data[0]  = ((0x02<<8)&0xff00) + ((0xBB<<0)&0x00ff);
	      OneMessage.FormatMsg.Data[1]  = uds_can_crc&0xffff;
	      OneMessage.FormatMsg.Data[2]  = (uds_can_crc>>16)&0xffff;
	      OneMessage.FormatMsg.Data[3]  = (((uint16)0<<8)&0xff00) + (((uint16)0<<0)&0x00ff);     
	      SendOneMessage(&OneMessage,MCU_TX);
		}	

		CanLoadInfoUse.LoadResetCounter = 0;
		CanLoadInfoUse.RxSum = 0;
		CanLoadInfoUse.LoadStep = 0;
		CanLoadInfoUse.LoadPadeCounter = 0;
		CanLoadInfoUse.ReciveLen = 0;
		uds_crc = 0xffffffff;
        break;

	default:

	   break;
  }
}


//串口升级主逻辑
uint32 CountInPage = 0;
void UartUpLoadMain(void)
{
 uint16 k=0;
 uint32 rcrc=0;
 uint32_t secstart;
 uint32_t secend;
 
  if(SciLoadInfoUse.RxSuccess)  //成功接受到主机请求 
  {
    SciLoadInfoUse.RxSuccess = 0;
	switch(SciLoadInfoUse.LoadStep)
	{
      case 0:  //启动升级
         if(0xFC == SciLoadInfoUse.RxData[0] \
		 && 0xCF == SciLoadInfoUse.RxData[1] \
		 && 0x02 == SciLoadInfoUse.RxData[3] \
		 && 0x00 == SciLoadInfoUse.RxData[4] \
		 && 0x00 == SciLoadInfoUse.RxData[5] \
		 && 0x00 == SciLoadInfoUse.RxData[6] \
		 && 0x00 == SciLoadInfoUse.RxData[7])
           {
			 secstart = GetSector(APP_START_ADDRESS);
			 secend = GetSector(FMC_FLASH_END);
			 TIM_Cmd(TIM6,DISABLE);
			 for (k = secstart; k < secend; k += 8) 
	          {
	            FLASH_Unlock();
				FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR\
					            |FLASH_FLAG_PGAERR|FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
	            FLASH_EraseSector(k,VoltageRange_3);
				FLASH_Lock();
			  }
			 
			 CountInPage = 0;
			 uds_crc = 0xffffffff;
			 SciLoadInfoUse.LoadStep = 1;
			 DevInfoCur.AppUpgradeFlag = 1;
			 UpdateDevInfo();
			 TIM_Cmd(TIM6,ENABLE);

			 //PC报文发送
             gPCCOMM_TxValueTab[0] = 0xFC;
			 gPCCOMM_TxValueTab[1] = 0xCF;
			 gPCCOMM_TxValueTab[2] = 0x01;
			 gPCCOMM_TxValueTab[3] = 0xAA;
			 gPCCOMM_TxValueTab[4] = 0x50;
			 gPCCOMM_TxValueTab[5] = 0x90;
			 gPCCOMM_TxValueTab[6] = 0xFE;
			 gPCCOMM_TxValueTab[7] = 0x00;
			 gPCCOMM_TxValueTab[8] = 0x89;
			 gPCCOMM_TxValueTab[9] = 0xED;
		     DMA_SetCurrDataCounter(DMA2_Stream6,10); 
		     DMA_Cmd(DMA2_Stream6,ENABLE);
		   }
		break;

	  case 1: 
         if( 0xFD == SciLoadInfoUse.RxData[0] \
   		  && 0xDF == SciLoadInfoUse.RxData[1] \
   		  && 0x01 == SciLoadInfoUse.RxData[3])     //开始升级烧录
           {
             SciLoadInfoUse.LoadResetCounter = 0;
             for(k=0;k<32;k++)
              {
                 SciLoadInfoUse.RxDataHalfWord[k] = (((uint16)SciLoadInfoUse.RxData[6+k*2]<<8)&0xff00)\
				 	                               + ((uint16)SciLoadInfoUse.RxData[5+k*2]&0x00ff);
			  } 
             TIM_Cmd(TIM6,DISABLE);						
	         STMFLASH_Write_NoCheck(APP_START_ADDRESS + CountInPage*64,&SciLoadInfoUse.RxDataHalfWord[0],32);            
		     //TIM_Cmd(TIM6,ENABLE);	
			 CountInPage++;
			 
			 //CRC计算
		     uds_crc = CRC32(&SciLoadInfoUse.RxData[5], 64, uds_crc);
			 TIM_Cmd(TIM6,ENABLE);

			 //PC报文发送
             gPCCOMM_TxValueTab[0] = 0xFD;
			 gPCCOMM_TxValueTab[1] = 0xDF;
			 gPCCOMM_TxValueTab[2] = 0x01;
			 gPCCOMM_TxValueTab[3] = 0xAA;
			 gPCCOMM_TxValueTab[4] = 0x00;
			 gPCCOMM_TxValueTab[5] = 0x00;
			 gPCCOMM_TxValueTab[6] = 0x00;
			 gPCCOMM_TxValueTab[7] = 0x00;
			 gPCCOMM_TxValueTab[8] = 0x00;
			 for(k=2;k<=7;k++)
		       {gPCCOMM_TxValueTab[8] += gPCCOMM_TxValueTab[k];}
			 gPCCOMM_TxValueTab[9] = 0xED;
		     DMA_SetCurrDataCounter(DMA2_Stream6,10); 
		     DMA_Cmd(DMA2_Stream6,ENABLE);
   		   }
		 else if( 0xFD == SciLoadInfoUse.RxData[0] \
   		  && 0xDF == SciLoadInfoUse.RxData[1] \
   		  && 0x02 == SciLoadInfoUse.RxData[3])     //开始校验
           {
             SciLoadInfoUse.LoadResetCounter = 0;
             rcrc = ((uint32)SciLoadInfoUse.RxData[8]<<24) + ((uint32)SciLoadInfoUse.RxData[7]<<16)\
			 	   +((uint32)SciLoadInfoUse.RxData[6]<<8) + ((uint32)SciLoadInfoUse.RxData[5]<<0);

			 if( (rcrc == uds_crc)\
			 	&&( memcmp( DevInfoCur.HardVersion, ((uint8_t *)APP_CHECK_ADDRESS + 8), sizeof(DevInfoCur.HardVersion) ) == 0 ))  //升级成功
			 {
               //PC报文发送
                 gPCCOMM_TxValueTab[0] = 0xFD;
    			 gPCCOMM_TxValueTab[1] = 0xDF;
    			 gPCCOMM_TxValueTab[2] = 0x01;
    			 gPCCOMM_TxValueTab[3] = 0xAA;
    			 gPCCOMM_TxValueTab[4] = 0x00;
    			 gPCCOMM_TxValueTab[5] = 0x00;
    			 gPCCOMM_TxValueTab[6] = 0x00;
    			 gPCCOMM_TxValueTab[7] = 0x00;
    			 gPCCOMM_TxValueTab[8] = 0x00;
    			 for(k=2;k<=7;k++)
    		       {gPCCOMM_TxValueTab[8] += gPCCOMM_TxValueTab[k];}
    			 gPCCOMM_TxValueTab[9] = 0xED;
    		     DMA_SetCurrDataCounter(DMA2_Stream6,10); 
    		     DMA_Cmd(DMA2_Stream6,ENABLE);

				 DevInfoCur.AppUpgradeFlag = 0;
			     UpdateDevInfo();
			 }
			 else
			 {
               //PC报文发送
                 gPCCOMM_TxValueTab[0] = 0xFD;
    			 gPCCOMM_TxValueTab[1] = 0xDF;
    			 gPCCOMM_TxValueTab[2] = 0x01;
    			 gPCCOMM_TxValueTab[3] = 0xEE;
    			 gPCCOMM_TxValueTab[4] = 0x00;
    			 gPCCOMM_TxValueTab[5] = 0x00;
    			 gPCCOMM_TxValueTab[6] = 0x00;
    			 gPCCOMM_TxValueTab[7] = 0x00;
    			 gPCCOMM_TxValueTab[8] = 0x00;
    			 for(k=2;k<=7;k++)
    		       {gPCCOMM_TxValueTab[8] += gPCCOMM_TxValueTab[k];}
    			 gPCCOMM_TxValueTab[9] = 0xED;
    		     DMA_SetCurrDataCounter(DMA2_Stream6,10); 
    		     DMA_Cmd(DMA2_Stream6,ENABLE);
			 }	

			 SciLoadInfoUse.LoadStep = 0;
   		   }
		 else
		   {
               //PC报文发送
                 gPCCOMM_TxValueTab[0] = 0xFD;
    			 gPCCOMM_TxValueTab[1] = 0xDF;
    			 gPCCOMM_TxValueTab[2] = 0x01;
    			 gPCCOMM_TxValueTab[3] = 0xEE;
    			 gPCCOMM_TxValueTab[4] = 0x00;
    			 gPCCOMM_TxValueTab[5] = 0x00;
    			 gPCCOMM_TxValueTab[6] = 0x00;
    			 gPCCOMM_TxValueTab[7] = 0x00;
    			 gPCCOMM_TxValueTab[8] = 0x00;
    			 for(k=2;k<=7;k++)
    		       {gPCCOMM_TxValueTab[8] += gPCCOMM_TxValueTab[k];}
    			 gPCCOMM_TxValueTab[9] = 0xED;
    		     DMA_SetCurrDataCounter(DMA2_Stream6,10); 
    		     DMA_Cmd(DMA2_Stream6,ENABLE);

				 SciLoadInfoUse.LoadStep = 0;
		   }
		break;
	}
  }
}

//串口接收
void PCUSART_IRQ0p1ms(void)
{
 static uint   RxOverCounter = 0;
 static uint   RNUM=0;  //接收byte数
 static uint   RNUMlast=0;  //接收byte数
 uint RxNum=0;
 uint k=0;
 uint i=0;

  //等待DMA2_Steam7传输完成
  if(DMA_GetFlagStatus(DMA2_Stream6,DMA_FLAG_TCIF6)!=RESET)
  {
	DMA_ClearFlag(DMA2_Stream6,DMA_FLAG_TCIF6); //清除DMA2_Steam7传输完成标志
	DMA_Cmd(DMA2_Stream6,DISABLE);
  }

  RNUMlast = RNUM;	 
  RNUM = 512 - DMA_GetCurrDataCounter(DMA2_Stream2); //获得接收到的字节数

  if(RxOverCounter++ >= 10)  //1ms没有新数据，一帧报文结束
  	{SciRxCounter = 0;}

  if(RNUM >= RNUMlast)
  {
     for(i=RNUMlast;i<RNUM;i++)
      {
        SciRx[SciRxCounter++] = gPCCOMM_RxValueTab[i]&0xff;
		RxOverCounter = 0;
      }
  }
  else
  {
     for(i=RNUMlast;i<512;i++)
      {
        SciRx[SciRxCounter++] = gPCCOMM_RxValueTab[i]&0xff;
		RxOverCounter = 0;
      }

	 for(i=0;i<RNUM;i++)
      {
        SciRx[SciRxCounter++] = gPCCOMM_RxValueTab[i]&0xff;
		RxOverCounter = 0;
      }
  }

  if(0 == SciLoadInfoUse.LoadStep)
  	{RxNum = 10;}
  else
  	{RxNum = 71;}

  if(SciRxCounter == RxNum) //一帧报文接受成功结束
  	{
  	   SciRxCounter = 0;
	   memcpy(&SciLoadInfoUse.RxData[0],&SciRx[0],RxNum); 
	   SciLoadInfoUse.RxSum = 0;
	   for(k=2;k<=RxNum-3;k++)
		{SciLoadInfoUse.RxSum += SciLoadInfoUse.RxData[k];}
	   
	   if((0xFC == SciLoadInfoUse.RxData[0] || 0xFD == SciLoadInfoUse.RxData[0])\
	   	 && 0x01 == SciLoadInfoUse.RxData[2]\
	   	 && SciLoadInfoUse.RxSum == SciLoadInfoUse.RxData[RxNum-2]\
	   	 && 0xED == SciLoadInfoUse.RxData[RxNum-1])
	   	{
	   	  SciLoadInfoUse.RxSuccess = 1;
		  SciLoadInfoUse.RxSum = 0;
		}
	   else
	   	{
	   	  SciLoadInfoUse.RxSuccess = 0;
		  SciLoadInfoUse.RxSum = 0;
		}
    }
}


// EOF-------

